#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "BlockQueue.hpp"
#include "Task.hpp"

void *consumer(void *args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        Task t = bq->Pop();
        t.Exec();
        std::cout << "消费了一个任务:" << t.X() << "+" << t.Y() << "=" << t.Res() << std::endl;
    }
    return 0;
}

void *productor(void *args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        int x = 1, y = 2;
        Task t(x, y);
        bq->Equeue(t);
        std::cout << "生产了一个数据:" << x << "+" << y << "=?" << std::endl;
        sleep(1);
    }
    return 0;
}

int main()
{
    //申请阻塞队列
    BlockQueue<Task> *bq = new BlockQueue<Task>();

    //构建生产者与消费者
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    return 0;
}